package LeetCode.leetcode2;


class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        long sum1 = 0;
        long index = 1;
        long sum2 = 0;
        while (l1 != null) {
            long val = l1.val;
            sum1 += val * index;
            index *= 10;
            l1 = l1.next;
        }
        index = 1;
        while (l2 != null) {
            long  val = l2.val;
            sum2 += val * index;
            index *= 10;
            l2 = l2.next;
        }
        long res = sum1 + sum2;
        ListNode resNode = new ListNode();
        ListNode cur = resNode;
        String resStr = String.valueOf(res);
        for (int i = resStr.length() - 1; i >= 0; i--) {
            char curChar =  resStr.charAt(i);
            cur.next = new ListNode(curChar - 48);
            cur = cur.next;
        }
        return resNode.next;

//        ListNode resNode = new ListNode();
//        ListNode cur = resNode;
//        while ((res / 10) > 0){
//            cur.next = new ListNode((int) (res % 10));
//            cur = cur.next;
//            res = res / 10;
//        }
//        cur.next = new ListNode((int) res);
//        return resNode.next;
    }
}